package prickworlds.game;

import prickworlds.Prickworlds;
import prickworlds.game.templates.RobotTemplate;

/**
 * Base class for every robot with artificial intelligence
 * 
 * @author Faruk Acibal, Richard Dziambor, Christian Holzreuter, Nicolas Winkler
 * 
 */
public class EnemyRobot extends Robot {
	/**
	 * the {@link Map} where the robot looks for his enemies
	 */
	protected Map map;

	/**
	 * sets an {@link EnemyRobot} to the specified position
	 * 
	 * @param posX
	 * @param posY
	 */
	public EnemyRobot(Map map, RobotTemplate template, float posX, float posY,
			int maxHitPoints, int team) {
		super(template, posX, posY, maxHitPoints, team);
		this.map = map;
	}

	/**
	 * sets the {@link Map} on which the Robot is moving
	 * 
	 * @param game
	 */
	public void setGame(Map map) {
		this.map = map;
	}

	public void destroy() {
		Prickworlds.getGame().getMap().getPlayer().score += 1;
		super.destroy();
	}

	/**
	 * updates the {@link EnemyRobot}
	 */
	@Override
	public void update() {
		velX = -posX + map.getPlayer().posX;
		velY = -posY + map.getPlayer().posY;

		float shortener = 0.01f / (float) Math.sqrt(velX * velX + velY * velY);
		velX *= shortener * template.getMaxSpeed();
		velY *= shortener * template.getMaxSpeed();

		targetX = map.getPlayer().posX;
		targetY = map.getPlayer().posY;
		
		setLookAt(targetX, targetY);

		requestShot();

		super.update();
	}
}
